package com.twt.recruit.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.twt.recruit.entity.*;
import com.twt.recruit.mapper.*;
import com.twt.recruit.utils.TimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ApplicationService {

    @Resource
    UserMapper userMapper;

    @Resource
    ApplicationMapper applicationMapper;

    @Resource
    GroupMapper groupMapper;

    @Resource
    ApplicationInfoMapper applicationInfoMapper;

    @Resource
    ApplicationCommentMappper applicationCommentMappper;

    @Resource
    ProjectGroupMapper projectGroupMapper;

    @Resource
    CommentMapper commentMapper;

    @Resource
    AnswerMapper answerMapper;

    @Resource
    QuestionMapper questionMapper;

    @Resource
    ProjectMapper projectMapper;

    @Resource
    ClubMapper clubMapper;


    public JSONObject getSingleApplication(String uid, Integer projectId) throws Exception{
        LambdaQueryWrapper<Application> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Application::getProjectId, projectId);
        queryWrapper.eq(Application::getApplicationUid, uid);
        Application application = applicationMapper.selectOne(queryWrapper);
        return dealApplication(application);
    }
    public JSONObject getSingleApplication(Integer applicationId) throws Exception{
        Application application = applicationMapper.selectById(applicationId);
        return dealApplication(application);

    }

    public JSONObject dealApplication(Application application) throws Exception{
        JSONObject re = JSONUtil.parseObj(application, false);
        Integer applicationId = application.getApplicationId();
        String uid = application.getApplicationUid();
        User user = userMapper.selectById(uid);
        //基本信息
        re.putOpt("name", user.getName());
        re.putOpt("major", user.getMajor());
        re.putOpt("department", user.getDepartment());
        re.putOpt("stuType", user.getStuType());
        LambdaQueryWrapper<ApplicationInfo> applicationInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        applicationInfoLambdaQueryWrapper.eq(ApplicationInfo::getApplicationId, applicationId);

        JSONArray groups = new JSONArray();
        Integer projectId = application.getProjectId();
        //组别及其问题答案
        for (ApplicationInfo applicationInfo : applicationInfoMapper.selectList(applicationInfoLambdaQueryWrapper)) {
            JSONObject g = JSONUtil.parseObj(applicationInfo);
            Integer groupId = applicationInfo.getGroupId();
            g.putOpt("groupName", groupMapper.selectById(groupId).getGroupName());
            g.putOpt("categories", JSONUtil.parseArray(applicationMapper.listCategoriesByApplicationId(applicationId, groupId)));
            LambdaQueryWrapper<ProjectGroup> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProjectGroup::getProjectId, projectId);
            queryWrapper.eq(ProjectGroup::getGroupId, groupId);
            g.putOpt("wantedGroupOrder", projectGroupMapper.selectOne(queryWrapper).getGroupOrder());
            JSONArray questions = new JSONArray();
            for (Integer i : questionMapper.questionIdsByProjectAndGroupId(projectId, groupId)) {
                JSONObject question = JSONUtil.parseObj(getSingleQuestion(i));
                Answer answer = answerMapper.answerIdByQuestionIdAndUid(uid, i);
                question.putOpt("answerId", answer.getAnswerId());
                question.putOpt("answerContent", answer.getAnswerContent());
                questions.add(question);
            }
            g.putOpt("questions", questions);
            groups.add(g);
        }
        re.putOpt("groups", groups);

        //评论信息
        List<List<String>> comments = new ArrayList<>();
        LambdaQueryWrapper<ApplicationComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApplicationComment::getApplicationId, applicationId);
        List<ApplicationComment> applicationComments = commentMapper.selectList(queryWrapper);
        if (!applicationComments.isEmpty()){
            for (ApplicationComment applicationComment : applicationComments) {
                List<String> comment = new ArrayList<>();
                comment.add(TimeUtil.timestampToDate(applicationComment.getUpdatedTime().getTime(), TimeUtil.Y_M_D_H_M));
                comment.add(userMapper.selectById(applicationComment.getOperationUid()).getName());
                comment.add(applicationComment.getCommentContent());
                comments.add(comment);
            }
        }
        re.putOpt("comments", comments);
        //项目名称和社团名称
        Project project = projectMapper.selectById(application.getProjectId());
        re.putOpt("projectName", project.getTitle());
        re.putOpt("clubName", clubMapper.selectById(project.getClubId()).getName());
        return re;

    }



    public Question getSingleQuestion(Integer questionId){

        return questionMapper.selectById(questionId);
    }

    public void delApplicationByProjectId(Integer projectId) {
        applicationMapper.delApplicationByProjectId(projectId);
    }

    public void delApplicationInfoByProjectId(Integer projectId) {
        applicationInfoMapper.delApplicationInfoByProjectId(projectId);
    }

    public void delApplicationCommentByApplicationId(List<Integer> applicationIdList) {
        applicationCommentMappper.delApplicationCommentByApplicationId(applicationIdList);
    }
}
